const baseConfig = require('../base-config')
const ResponseModel = require('../models/response-model')
const jwtToken = require('../utils/jwt-token')

module.exports = {
    // 检测登录状态
    'get::/auth/check': (ctx) => {
        ctx.response.body = ResponseModel.success("123123123123")
    },

    // 登录
    'post::/auth/doAuth': async (ctx) => {
        const { passkey } = ctx.request.body
        if (passkey === baseConfig.passkey ) {
            ctx.response.body = ResponseModel.success(jwtToken.genToken(passkey))
        } else {
            ctx.response.body = ResponseModel.failure("密码错误")
        }
    },

    // 修改密码 
    'post::/auth/resetPasskey': async (ctx) => {
        const { oldPass, newPass } = ctx.request.body
        if (!oldPass || !newPass) {
            ctx.response.body = ResponseModel.failure("参数错误")
            return
        }
        if (oldPass !== baseConfig.passkey ) {
            ctx.response.body = ResponseModel.failure("原密码错误")
            return
        }
        // 修改
        baseConfig.passkey = newPass
        // TODO 持久化到文件
        //
        ctx.response.body = ResponseModel.success()
    },
}
  